import { Button, Modal, Space } from 'antd'

import { handleCopy } from '@/utils/utils'
import { InpTransferOrderNumModal } from '@oBiz/TrackOrder/components/InpTransferOrderNumModal'
import { SendCusEmailDrawer } from '@oBiz/TrackOrder/Detail/SendEmailDrawer'
import { eventBus, popupSlot, useUrlParams } from '../utils'
import { apiHandleAbnormal, apiHandleArchive } from './apis'
import { useBoundStore } from './store'

export const Actions = () => {
  const { waybillNo } = useUrlParams()
  const loading = useBoundStore(v => v.loading)
  const waybillInfo = useBoundStore(v => v.waybillInfo)
  const setWaybillInfo = useBoundStore(v => v.setWaybillInfo)
  const { currentLogisticsNode: curr, logisticsNodeList: list, abnormalStatus, archiveFlag } = waybillInfo || {}
  const currIndex = list?.findIndex(v => v === curr) || -1
  const isCurrLast = !!list && list.length - 1 === currIndex

  return (
    <Space size={4}>
      {$APP === 'TMS' && (
        <>
          {archiveFlag === 0 && abnormalStatus === -1 && (
            <Button
              type="primary"
              disabled={loading}
              onClick={() => {
                Modal.confirm({
                  title: '确认要标记处理中吗？',
                  onOk: async () => {
                    await apiHandleAbnormal({ waybillNo, abnormalStatus: 0 })
                    setWaybillInfo({ abnormalStatus: 0 })
                  },
                })
              }}
            >
              标记异常处理中
            </Button>
          )}
          {archiveFlag === 0 && (abnormalStatus === -1 || abnormalStatus === 0) && (
            <Button
              type="primary"
              disabled={loading}
              onClick={() => {
                Modal.confirm({
                  title: '确认要标记已处理吗？',
                  onOk: async () => {
                    await apiHandleAbnormal({ waybillNo, abnormalStatus: 1 })
                    setWaybillInfo({ abnormalStatus: 1 })
                  },
                })
              }}
            >
              标记异常已处理
            </Button>
          )}
          {archiveFlag === 0 && (
            <Button
              type="primary"
              onClick={() => {
                const modalUpdate = popupSlot.insert(
                  <InpTransferOrderNumModal
                    destroy={() => modalUpdate(null)}
                    onSuccess={() => eventBus.emit('transferOrderNumChanged')}
                  />,
                )
              }}
            >
              录入转单号
            </Button>
          )}
        </>
      )}
      {$APP === 'OMS' && (
        <>
          {archiveFlag === 0 && isCurrLast && (
            <Button
              danger
              disabled={loading}
              onClick={() => {
                Modal.confirm({
                  title: '确认要归档吗？',
                  content: '操作归档运单数据将不会出现在常规物流状态中，请谨慎操作。',
                  onOk: async () => {
                    await apiHandleArchive({ waybillNo, archiveType: 1 })
                    setWaybillInfo({ archiveFlag: 1 })
                  },
                })
              }}
            >
              归档
            </Button>
          )}
          {archiveFlag === 1 && isCurrLast && (
            <Button
              danger
              disabled={loading}
              onClick={() => {
                Modal.confirm({
                  title: '确认要取消归档吗？',
                  content: '取消归档后运单将重新出现在常规物流状态中，请谨慎操作。',
                  onOk: async () => {
                    await apiHandleArchive({ waybillNo, archiveType: 2 })
                    setWaybillInfo({ archiveFlag: 0 })
                  },
                })
              }}
            >
              取消归档
            </Button>
          )}
          <Button
            onClick={() => {
              const url = `${location.origin}/${import.meta.env.VITE_APP_BASE_NAME}/#/track/result?type=waybillNos&value=${waybillNo}`
              handleCopy(url)
            }}
          >
            复制链接
          </Button>
          <Button
            type="primary"
            onClick={() => {
              const modalUpdate = popupSlot.insert(<SendCusEmailDrawer destroy={() => modalUpdate(null)} />)
            }}
          >
            发送邮件
          </Button>
        </>
      )}
    </Space>
  )
}
